package com.reer;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends TabActivity {
    private static final String TAG = "REER-READER";
    private TabHost mHost;
    private Intent mListIntent;
    private static final Map<Integer, Drawable> mDrawableMap = new HashMap<Integer, Drawable>(5);
    private static final int DIALOG_PROGRESS = 0;
    private ProgressDialog mDialog;

    private void initMapData() {
        mDrawableMap.put(0, getResources().getDrawable(R.drawable.icon_1));
        mDrawableMap.put(1, getResources().getDrawable(R.drawable.icon_2));
        mDrawableMap.put(2, getResources().getDrawable(R.drawable.icon_3));
        mDrawableMap.put(3, getResources().getDrawable(R.drawable.icon_4));
        mDrawableMap.put(4, getResources().getDrawable(R.drawable.icon_5));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initMapData();
        mHost = this.getTabHost();
        mListIntent = new Intent(this, ListActivity.class);
        requestTypeListData();
        mHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            public void onTabChanged(String s) {
                notifyListActivityChangeData(s);
            }
        });
    }

    private void requestTypeListData(){
        showDialog(DIALOG_PROGRESS);
        for (int i = 1; i < 5; i++) {
            mHost.addTab(mHost.newTabSpec(""+i).setIndicator(createTabView("TAB" + i,i)).setContent(mListIntent));
        }
        notifyListActivityChangeData("1");
        hideProgressDialog();
    }

    private void notifyListActivityChangeData(String listKey){
        ListActivity.listKey=listKey;
        Message message=new Message();
        message.what=ListActivity.DATA_NOTIFY_MSG;
        ListActivity.mHandler.sendMessage(message);
    }

    private View createTabView(String text,int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
        TextView tv = (TextView) view.findViewById(R.id.tab_text_view);
        tv.setText(text);
        ImageView im=(ImageView)view.findViewById(R.id.tab_icon);
        im.setImageDrawable(mDrawableMap.get(i));
        return view;
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
